﻿
/**
 * @brief 表格窗体与处理器声明
 * @author LiBaojun (libaojunqd@foxmail.com)
 * @date 2024-12-05
 */
#ifndef __WIDGETBROWSERTABLE_H__
#define __WIDGETBROWSERTABLE_H__

#include <QTableWidget>
#include "GUIWidgetAPI.h"

class QPushButton;
class QMenu;

namespace Acoustics {
    class FITKAcousticsPhysics;
}

namespace GUI
{
    class BrowserTableAbstractHandler;
    /**
     * @brief 表格浏览器定义
     * @author LiBaojun (libaojunqd@foxmail.com)
     * @date 2024-12-05
     */
    class GUIWIDGETAPI WidgetBrowserTable : public QTableWidget
    {
        Q_OBJECT
    public:
        /**
         * @brief Construct a new Widget Browser Table object
         * @param[i]  parent         父对象
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        explicit WidgetBrowserTable(QWidget* parent);
        /**
         * @brief Destroy the Widget Browser Table object
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual ~WidgetBrowserTable();
         /**
         * @brief 更新表格
         * @param[i]  h         处理器
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */ 
        void update(BrowserTableAbstractHandler* h = nullptr);
         /**
         * @brief 自动调整单元格宽度
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        void autoResizeCellWidth();
        /**
         * @brief 设置单元格可编辑，默认参数表示整个表格
         * @param[i]  e         是否可编辑
         * @param[i]  row         行索引 -1 表示所有行
         * @param[i]  col         列索引  -1表示所有列
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-10
         */
        void setTableItemEditable(bool e, const int &row = -1, const int &col =-1 );

    private slots:
        /**
         * @brief 右键菜单槽函数
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        void contextMenuSlot(const QPoint&);
        /**
         * @brief 表头右键菜单槽函数
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        void headerContextMenuSlot(const QPoint&);
        /**
         * @brief 双击单元格槽函数
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        void itemDoubleClicked(QTableWidgetItem *item);

        /**
         * @brief 单击单元格槽函数
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        void itemClicked(QTableWidgetItem *item);

    private:
        /**
         * @brief 处理器
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        BrowserTableAbstractHandler* _handler{};
    };


    /**
     * @brief 表格处理器抽象类
     * @author LiBaojun (libaojunqd@foxmail.com)
     * @date 2024-12-05
     */
    class GUIWIDGETAPI BrowserTableAbstractHandler : public QObject
    {
        Q_OBJECT
    public:
        /**
         * @brief Construct a new Browser Table Abstract Handler object
         * @param[i]  table         表格
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        explicit BrowserTableAbstractHandler(WidgetBrowserTable* table);
        /**
         * @brief 析构
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual ~BrowserTableAbstractHandler() = 0;
        /**
         * @brief 更新表格，包含头与数据
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual void updateTable()= 0;
        /**
         * @brief 更新数据
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual void updateData();
        /**
         * @brief 右键菜单
         * @param[i]  popMenu         右键菜单
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */ 
        virtual void contextMenu(QMenu* popMenu);
        /**
         * @brief 表头右键菜单
         * @param[i]  popMenu         右键菜单
         * @param[i]  coluIndex         列索引
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual void headerContextMenu(QMenu* popMenu, int coluIndex);
         /**
         * @brief 单元格双击
         * @param[i]  row         行索引
         * @param[i]  colum         列索引
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        virtual void itemDoubleClicked(int row, int colum);
        /**
         * @brief 单元格单击
         * @param[i]  row         行索引
         * @param[i]  colum         列索引
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-06
         */
        virtual void itemClicked(int row, int colum);

    protected slots:
        /**
         * @brief 作为纯文本复制到剪贴板
         * @author wangning (2185896382@qq.com)
         * @date 2025-03-06
         */
        void copyAsTextSlot();

    protected:
        /**
         * @brief 获取选中单元格
         * @return QList<QTableWidgetItem* >         选中单元格
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        QList<QTableWidgetItem* > getSelectItems();
        /**
         * @brief 获取选中单元格
         * @return QTableWidgetItem*         选中单元格
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        QTableWidgetItem* getSelectItem();
        /**
         * @brief 给菜单添加action
         * @param[i]  menu           菜单
         * @param[i]  text           action文本
         * @return QAction* 
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        static QAction* AddMenuAction(QMenu* menu, const QString& text);
        /**
       * @brief 设置按钮背景色
       * @param[i] button 按钮
       * @param[i] r g b  颜色
       * @author LiBaojun (libaojunqd@foxmail.com)
       * @date 2024-12-19
       */
        static void SetPushButtonColor(QPushButton* button, int r, int g, int b);
 

    protected:
        /**
         * @brief 表格对象
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-12-05
         */
        WidgetBrowserTable* _tableWidget{};
        /**
         * @brief  声学物理数据对象
         * @author BaGuijun (baguijun@163.com)
         * @date   2024-12-06
         */
        Acoustics::FITKAcousticsPhysics* _physics = nullptr;
    };

}
#endif // WIDGETBROWSERTABLE_H
